package biz.innovasoft.nodb.transaction;

import java.util.Date;

import org.prevayler.Transaction;

import biz.innovasoft.nodb.impl.GenericRepository;
import biz.innovasoft.nodb.index.DateResolution;

public class CreateIndexTransaction implements Transaction {

	private static final long serialVersionUID = 1L;
	
	private Class<?> clazz;
	private String indexPath;
	private boolean allowNulls = true;
	private DateResolution resolution;
	
	
	public CreateIndexTransaction (Class<?> clazz, String indexPath) {
		this.clazz = clazz;
		this.indexPath = indexPath;
	}
	
	public CreateIndexTransaction (Class<?> clazz, String indexPath, boolean allowNulls) {
		this.clazz = clazz;
		this.indexPath = indexPath;
		this.allowNulls = allowNulls;
	}

	public CreateIndexTransaction (Class<?> clazz, String indexPath, DateResolution resolution) {
		this.clazz = clazz;
		this.indexPath = indexPath;
		this.resolution = resolution;
	}

	
	@Override
	public void executeOn(Object prevalentSystem, Date time) {
		GenericRepository repository = (GenericRepository)prevalentSystem;
		if ( resolution == null ) {
			repository.createIndex(clazz, indexPath, allowNulls);
		} else {
			repository.createIndex(clazz, indexPath, resolution, allowNulls);
		}
	}

}
